package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.oocl.dao.DishDao;
import com.oocl.pojo.Dish;
import com.oocl.pojo.DishSearchCondition;
import com.oocl.util.DBUtil;

public class DishDaoImpl implements DishDao{

	@Override
	public int addDish(Dish d) {
		String sql="insert into dish(did,dname,dprice,dimage,dtype,mid)values(?,?,?,?,?,?)";
		Connection conn=null;
		PreparedStatement pst =null;
		UUID uuid = null;
		String dId = null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			uuid = UUID.randomUUID();
			dId = uuid.toString();
			pst.setString(1, dId);
			pst.setString(2, d.getdName());
			pst.setDouble(3, d.getdPrice());
			pst.setString(4, d.getdImage());
			pst.setInt(5, d.getdType());
			pst.setString(6, d.getmId());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public int updateDish(Dish d) {
		String sql="update dish set dname=?,dprice=?,dimage=?,dtype=?,mid=? where did =?";
		PreparedStatement pst =null;
		Connection conn = null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, d.getdName());
			pst.setDouble(2, d.getdPrice());
			pst.setString(3, d.getdImage());
			pst.setInt(4, d.getdType());
			pst.setString(5, d.getmId());
			pst.setString(6, d.getdId());
			n = pst.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public int deleteDish(String dId) {
		String sql="delete dish where did =?";
		Connection conn=null;
		PreparedStatement pst =null;
		int n=0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, dId);
			n=pst.executeUpdate();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			DBUtil.release(null, pst, conn);
		}
		return n;
	}

	@Override
	public List<Dish> findAllDishes(String mId) {
		List<Dish> dl = new ArrayList<Dish>();
		String sql="select * from dish where mid = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
				dl.add(d);
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return dl;
	}


	@Override
	public List<Dish> findDishesByType(Integer dType, String mId) {
		List<Dish> dl = new ArrayList<Dish>();
		String sql="select * from dish where dtype = ? and mid = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setInt(1, dType);
			pst.setString(2, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
				dl.add(d);
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return dl;
	}

	@Override
	public List<Dish> findDishesByName(String dName, String mId) {
		List<Dish> dl = new ArrayList<Dish>();
		String sql="select * from dish where dname = ? and mid = ?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, dName);
			pst.setString(2, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
				dl.add(d);
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return dl;
	}

	@Override
	public List<Dish> findDishesByBoth(Integer dType, String dName, String mId) {
		List<Dish> dl = new ArrayList<Dish>();
		String sql="select * from dish where dtype=? and dname=? and mid=?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setInt(1, dType);
			pst.setString(2, dName);
			pst.setString(3, mId);
			rs=pst.executeQuery();
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
				dl.add(d);
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return dl;
	}
	
	@Override
	public Dish findDishById(String dId) {
		String sql="select * from dish where did =?";
		PreparedStatement pst =null;
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		int m =0;
		try {
			conn=DBUtil.getConnection();
			pst=conn.prepareStatement(sql);
			pst.setString(1, dId);
			rs=pst.executeQuery();
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
			}
			m=pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.release(rs, pst, conn);
		}
		return d;
	}

	@Override
	public List<Dish> findDishByCondition(String conditionSql) {
		if(null==conditionSql){
			return null;
		}
		ResultSet rs = null;
		Connection conn = null;
		Dish d =null;
		Statement stmt=null;
		List<Dish> dishes = new ArrayList<>();
		try {
			conn=DBUtil.getConnection();
			stmt = conn.createStatement();
			rs = stmt.executeQuery(conditionSql);
			while(rs.next()){
				d=new Dish();
				d.setdId(rs.getString("did"));
				d.setdName(rs.getString("dname"));
				d.setdPrice(rs.getDouble("dprice"));
				d.setdImage(rs.getString("dimage"));
				d.setdType(rs.getInt("dtype"));
				d.setmId(rs.getString("mid"));
				dishes.add(d);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			if(null!=stmt){
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			DBUtil.release(rs, null, conn);
		}
		return dishes;
	}

	

}
